package ua.com.globallogic.basecamp.krasnyanskiy.FileManager;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import static java.lang.System.getProperties;
import static java.lang.System.out;

class ConsoleProcessor implements Processor {
    private final FileManager manager = new ConsoleFileManager();
    Map<String, Command> map = new HashMap<String, Command>();

    {
        //todo: Def all possible commands

        map.put("ls", new LSCommand());
        map.put("..", new GetParentCommand());
    }

    @Override
    public void start() {
        out.println(getProperties().getProperty("user.dir"));
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            try {
                Command cmd = parse(in.nextLine());
                cmd.execute();
            } catch (ExitException ignored) {
                break;
            }
        }
        in.close();
    }

    //todo: Create Factory Method
    //todo: Private class Factory ???

    private Command parse(String line) {
        String[] exp = line.trim().split(" ");
        if (exp.length == 1)
            return new SimpleCommand(exp[0], null, manager);
        else
            return new SimpleCommand(exp[0], exp[1], manager);
    }
}
